import { getAuthUrl,getAuthUser,checkToken,refreshToken,logout } from '@/api/wechat'
import { getToken, setToken,
    getRefreshToken, setRefreshToken,
    setAppId,getAppId } from '@/utils/auth'
import website from "@/const/website";
const user = {
    state: {
        appId: getAppId(),
        token: getToken(),
        refresh_token: getRefreshToken(),
        name: '',
        avatar: '',
        roles: [],
        permissions: []
    },

    mutations: {
        SET_APPID: (state, appId) => {
            state.appId = appId;
            setAppId(appId);
        },
        SET_TOKEN: (state, token) => {
            state.token = token;
            setToken(token);
        },
        SET_REFRESH_TOKEN: (state, rfToken) => {
            state.refresh_token = rfToken;
            setRefreshToken(rfToken);
        },
        SET_NAME: (state, name) => {
            state.name = name
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles
        },
        SET_PERMISSIONS: (state, permissions) => {
            state.permissions = permissions
        }
    },

    actions: {
        //获取授权链接
        GetAuthUrl({commit},authInfo){
            return new Promise((resolve, reject) => {
                getAuthUrl(authInfo).then(res => {
                    commit('SET_APPID', authInfo.appId);
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        //获取用户信息
        GetAuthUser({commit},obj){
            return new Promise((resolve, reject) => {
                getAuthUser(obj).then(res => {
                    if (res.data.access_token){
                        commit('SET_TOKEN', res.data.access_token)
                        commit('SET_REFRESH_TOKEN', res.data.refresh_token)
                        commit('SET_NAME', res.data.user_info.socialUser.display_name)
                        commit('SET_AVATAR', res.data.user_info.socialUser.imageUrl)
                    }
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },
        CheckToken({commit, state},obj){
            return new Promise((resolve, reject) => {
                checkToken(state.token).then(res => {
                    const expire = res && res.data && res.data && res.data.exp;
                    if (expire) {
                        const expiredPeriod = expire * 1000 - new Date().getTime();
                        //小于半小时自动续约
                        if (expiredPeriod <= website.remainingTime) {
                            console.log('刷新，token过期时间', expiredPeriod, '毫秒');
                            refreshToken(state.refresh_token).then(res => {
                                commit('SET_TOKEN', res.data.access_token)
                                commit('SET_REFRESH_TOKEN', res.data.refresh_token)
                                resolve()
                            }).catch(error => {
                                clearInterval(this.refreshTime);
                                reject(error)
                            })
                        }else {
                            resolve()
                        }
                    }else {
                        resolve()
                    }
                }).catch(error => {
                    clearInterval(this.refreshTime)
                    reject(error)
                })
            })
        },
        Logout({commit},obj){
            return new Promise((resolve, reject) => {
                logout().then((res)=>{
                    if (res.data.code==200){
                        commit('SET_TOKEN', '');
                        commit('SET_REFRESH_TOKEN', '');
                        commit('SET_NAME', '');
                        commit('SET_AVATAR', '');
                    }
                }).catch(error=>{
                })
            })
        }
    }
}

export default user
